
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
* SET GLOBAL $PATHS
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
global root     = "INSERT CUSTOMIZED PATH"
global source   = "$root/code/calibrations/results/"
global data 	= "$root/data"
global data_out = "$root/data/processed/HealthProcess"
global figures	= "$root/figsandtabs"
global tables	= "$root/figsandtabs"
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

set scheme s1mono
graph set window fontface "Garamond"

global size size(large)
global ls labsize(medlarge)
global legs medlarge

**************************
/* Table 1 and Figure A5*/
**************************

use "${data_out}/prcere7bmnl.dta", clear
_strip_labels agegr 

merge m:1 cere7 using "${data_out}/mcere7A.dta"

drop _merge

ren cere7 state

bysort agegr: egen tpop=total(npeople)

collapse expm fl1-fl8 [fw=npeople], by(state)

mkmat expm fl1-fl8, matrix(transmatb)


matrix rownames transmatb= 1 2 3 4 5 6 7

/* Table 1 */

estout matrix(transmatb, fmt(%9.0fc 3 3 3 3 3 3 3 3)) using "${tables}/transmatb.txt", style(tex) replace ///
mlabels(,none) collabels(,none) eqlabels(,none) 

drop expm

reshape long fl, i(state) j(fstate)

bysort state (fstate): gen fln=sum(fl)

reshape wide fl fln, i(fstate) j(state)

/* Figure A5 */

twoway (line fln1 fln2 fln3 fln4 fln5 fln6 fln7 fstate, lpattern(dot shortdash longdash solid shortdash longdash solid) lcolor(black gs7 gs7 gs7 black black black)),  ///
 legend(ring(0) position(4) cols(1)  size($legs) order(1 "{&lambda}{subscript:t}=1" 2 "{&lambda}{subscript:t}=2" 3 "{&lambda}{subscript:t}=3" 4 "{&lambda}{subscript:t}=4" 5 "{&lambda}{subscript:t}=5" 6 "{&lambda}{subscript:t}=6" 7 "{&lambda}{subscript:t}=7")) ///
  ytitle("F({&lambda}{subscript:t+1}|{&lambda}{subscript:t})", $size) xtitle("Destination Category ({&lambda}{subscript:t+1})", $size) ///
  xlabel(1 2 3 4 5 6 7 8, $ls) ylabel(0 .2 .4 .6 .8 1, $ls)
 

graph export "${figures}/stochdomB.pdf", replace

***********************
/* Figure A6*/
***********************

use "${data_out}/mcere7S.dta", clear
rename expm mx
merge m:1 cere7 using "${data_out}/mcere7A.dta"
drop _merge
reshape wide npeople mx expm, i(agegr) j(cere7)

tw (line mx1 agegr) (line mx2 agegr) (line mx3 agegr) (line mx4 agegr) (line mx5 agegr) (line mx6 agegr) (line mx7 agegr) ///
(line expm1 agegr, lpattern(dash)) (line expm2 agegr, lpattern(dash)) (line expm3 agegr, lpattern(dash)) (line expm4 agegr, lpattern(dash)) (line expm5 agegr, lpattern(dash)) (line expm6 agegr, lpattern(dash)) (line expm7 agegr, lpattern(dash)), ///
 legend(ring(0) pos(11) cols(1) off size($legs) order(3 "{&lambda}>2" 2 "{&lambda}=2" 1 "{&lambda}=1")) ///
 ytitle("Mean Expenditure", $size) xtitle("Age Group", $size) yscale(r(0 70000)) ///
 ylabel(0(20000)60000, $ls format( %12.0fc)) xlabel(0 "25-30" 2 "35-40" 4 "45-50" 6 "55-60" 8 "65-70", $ls)

graph export "${figures}/eml.pdf", replace


******************
* Tables A3-A4 */
******************

use "${data_out}/prcere7bmnl.dta", clear

sort agegr cere7

/* Tables A3-A4 */

levelsof agegr, local(agr)
local lbe : value label agegr

foreach aggr of local agr {
local agnm : label `lbe' `aggr'

mkmat fl* if agegr==`aggr', matrix(transmat)
local firstr="\multirow{5}{ * }{ " + "`agnm'" + "}"
di "`firstr'"
matrix rownames transmat="`firstr'" "" "" "" "" ""

matrix list transmat

estout matrix(transmat, fmt(3)) using "${tables}/transmat`aggr'yb.txt", style(tex) replace ///
mlabels(,none) collabels(,none) eqlabels(,none)  substitute(r2 "" r3 "" r4 "" r5 "" r6 "" r7 "")


}





